package com.example.friend;

import quickaction.ActionItem;
import quickaction.QuickAction;
import service.SmsSendService;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import db.MyDatabase;

public class MainActivity extends Activity {

	private static MyDatabase db;
	private static String[] from = new String[] { "phone", "name", "pass_day", "seen_event" };
	private static int[] to = new int[] { R.id.tvPhone, R.id.tvName, R.id.tvPassDay, R.id.tvSeenEvent };
	private Cursor c;

	private String selectedPhone;

	private QuickAction mQuickAction;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_startservice) {
			startSmsSendService();
			Toast.makeText(getBaseContext(), "Service started !", Toast.LENGTH_SHORT).show();
		} else if (id == R.id.action_stopservice) {
			stopSmsSendService();
			Toast.makeText(getBaseContext(), "Service stopped !", Toast.LENGTH_SHORT).show();
		} else if (id == R.id.action_sync_contacts) {
			db.syncContact();
		}
		// return super.onOptionsItemSelected(item);
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.ctmcall, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.itemCall:
			Log.i("makeCall", selectedPhone);
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onDestroy() {
		if (c != null)
			c.close();
		super.onDestroy();
	}

	// ////////////////////////////////////////////////////////////
	// Các hàm hỗ trợ
	//
	private void init() {
		/* ************** */
		/* init database */
		db = new MyDatabase(this);

		/* ************** */
		/* init tab host */
		// 1.Lay tab host
		TabHost th = (TabHost) findViewById(R.id.tabhost);
		th.setup(); // phải chạy lệnh này trước khi add tab
		th.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				if ("tab1".equals(tabId)) {
					loadTab1();
				} else if ("tab2".equals(tabId)) {
					loadTab2();
				} else if ("tab3".equals(tabId)) {
					loadTab3();
				}
			}
		});
		TabHost.TabSpec spec;
		spec = th.newTabSpec("tab1");
		spec.setContent(R.id.tab1);
		spec.setIndicator("1-Sad");
//		spec.setIndicator("1-Sad", getResources().getDrawable(R.drawable.ic_launcher));

		th.addTab(spec);

		spec = th.newTabSpec("tab2");
		spec.setContent(R.id.tab2);
		spec.setIndicator("2-Fun");

		th.addTab(spec);

		spec = th.newTabSpec("tab3");
		spec.setContent(R.id.tab3);
		spec.setIndicator("3-Cold");

		th.addTab(spec);
		th.setCurrentTab(0);

		/* ************** */
		/* init QuickAction */
		// Add action item
		ActionItem addAction = new ActionItem();

		addAction.setTitle("Phone");
		addAction.setIcon(getResources().getDrawable(R.drawable.phone));

		// Accept action item
		ActionItem smsAction = new ActionItem();

		smsAction.setTitle("Sms");
		smsAction.setIcon(getResources().getDrawable(R.drawable.sms));

		// Upload action item
		ActionItem delAction = new ActionItem();

		delAction.setTitle("Remove");
		delAction.setIcon(getResources().getDrawable(R.drawable.delete));

		mQuickAction = new QuickAction(this);

		mQuickAction.addActionItem(addAction);
		mQuickAction.addActionItem(smsAction);
		mQuickAction.addActionItem(delAction);

		// setup the action item click listener
		mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
			public void onItemClick(int pos) {

				if (pos == 0) { // Add item selected
					// Toast.makeText(MainActivity.this, "PHONE item selected",
					// Toast.LENGTH_SHORT).show();
					openCallDialer(selectedPhone);
					// makeCall(selectedPhone);
				} else if (pos == 1) { // Accept item selected
					// Toast.makeText(MainActivity.this, "GMAIL item selected",
					// Toast.LENGTH_SHORT).show();
					openSmsCompose(selectedPhone);
				} else if (pos == 2) { // Upload item selected
//					Toast.makeText(MainActivity.this, "REMOVE selected", Toast.LENGTH_SHORT).show();
					db.removeSeen(selectedPhone);
					loadTab2();
				}
			}
		});
	}

	private void loadTab1() {
		/*-TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		Toast.makeText(this, "Operator Code : " + tel.getSimOperator().toString()
				+ "\nOperator Name : " + tel.getSimOperatorName().toString()
				+ "\nCountry ISO : " + tel.getSimCountryIso().toString(), Toast.LENGTH_SHORT).show();*/

		Cursor c = db.getSadList();

		ListView lv = (ListView) findViewById(R.id.lvSad);
		lv.setAdapter(new SimpleCursorAdapter(this, R.layout.cl_contact, c, from, to));
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adap, View v, int arg2, long arg3) {
				selectedPhone = ((TextView) v.findViewById(R.id.tvPhone)).getText().toString();
				mQuickAction.show(v);
			}
		});
	}

	private void loadTab2() {
		Cursor c = db.getFunList();

		ListView lv = (ListView) findViewById(R.id.lvFun);
		lv.setAdapter(new SimpleCursorAdapter(this, R.layout.cl_contact, c, from, to));
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adap, View v, int arg2, long arg3) {
				selectedPhone = ((TextView) v.findViewById(R.id.tvPhone)).getText().toString();
				mQuickAction.show(v);
			}
		});
	}

	private void loadTab3() {
		Cursor c = db.getColdList();

		ListView lv = (ListView) findViewById(R.id.lvCold);
		lv.setAdapter(new SimpleCursorAdapter(this, R.layout.cl_contact, c, from, to));
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adap, View v, int arg2, long arg3) {
				selectedPhone = ((TextView) v.findViewById(R.id.tvPhone)).getText().toString();
				mQuickAction.show(v);
				// mQuickAction.setAnimStyle(QuickAction.ANIM_AUTO);
			}
		});
	}

	public void startSmsSendService() {
		startService(new Intent(getBaseContext(), SmsSendService.class));
	}

	public void stopSmsSendService() {
		stopService(new Intent(getBaseContext(), SmsSendService.class));
	}

	public static MyDatabase getDB() {
		return db;
	}

	private void makeCall(String phone) {
		Uri uri = Uri.parse("tel:" + phone);
		Intent i = new Intent(Intent.ACTION_CALL, uri);
		startActivity(i);
	}

	private void openCallDialer(String phone) {
		Intent intent = new Intent(Intent.ACTION_DIAL);
		intent.setData(Uri.parse("tel:" + phone));
		startActivity(intent);
	}

	private void openSmsCompose(String phone) {
		Intent smsIntent = new Intent(Intent.ACTION_VIEW);
		smsIntent.setType("vnd.android-dir/mms-sms");
		smsIntent.putExtra("address", phone);
		smsIntent.putExtra("sms_body", "");
		startActivity(smsIntent);
	}
}
